package com.hps.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hps.demo.entity.Order;
import com.hps.demo.service.IGoodService;
import com.hps.demo.service.IOrderService;
import com.hps.demo.service.IUserService;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author heps
 * @since 2020/10/30 10:34
 */
@RestController
@RequestMapping("/test")
public class TestController {

  @Resource
  private IOrderService orderService;

  @Resource
  private IUserService userService;

  @Resource
  private IGoodService goodService;

  private int currentPage = 100;

  private int size = 10;

  @GetMapping("/test-single")
  public String testSingle() {
    Page<Order> page = new Page<>(currentPage, size);
    long time1 = System.currentTimeMillis();
    Page<Order> result = orderService.page(page);
    long time2 = System.currentTimeMillis();
    for (Order order : result.getRecords()) {
      userService.getById(order.getUserId());
      goodService.getById(order.getGoodId());
    }
    long time3 = System.currentTimeMillis();
    System.out.println("list spend: " + (time2 - time1) + "ms");
    System.out.println("detail spend: " + (time3 - time2) + "ms");
    return "success";
  }

  @GetMapping("/test-cache")
  public String testCache() {
    Page<Order> page = new Page<>(currentPage, size);
    long time1 = System.currentTimeMillis();
    Page<Order> result = orderService.page(page);
    long time2 = System.currentTimeMillis();
    for (Order order : result.getRecords()) {
      userService.getByIdCache(order.getUserId());
      goodService.getByIdCache(order.getGoodId());
    }
    long time3 = System.currentTimeMillis();
    System.out.println("list spend: " + (time2 - time1) + "ms");
    System.out.println("detail spend: " + (time3 - time2) + "ms");
    return "success";
  }
}
